<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="姓名" prop="laborerId">
        <el-select v-model="queryParams.laborerId" placeholder="请选择劳务人员">
            <el-option
              v-for="dict in laborersList"
              :key="dict.id"
              :label="dict.name"
              :value="dict.id"
            ></el-option>
          </el-select>
      </el-form-item>
      <el-form-item label="薪资月份" prop="salaryMonth">
        <el-date-picker clearable
          v-model="queryParams.salaryMonth"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择薪资月份">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="总薪资" prop="totalSalary">
        <el-input
          v-model="queryParams.totalSalary"
          placeholder="请输入总薪资"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <!-- <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['ldcm:salaryrecords:add']"
        >新增</el-button> -->
      </el-col>
      <el-col :span="1.5">
        <!-- <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['ldcm:salaryrecords:edit']"
        >修改</el-button> -->
      </el-col>
      <el-col :span="1.5">
        <!-- <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['ldcm:salaryrecords:remove']"
        >删除</el-button> -->
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['ldcm:salaryrecords:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="salaryrecordsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" type="index" align="center" prop="recordId" />
      <el-table-column label="劳务人员姓名" align="center" prop="name" />
      <el-table-column label="薪资月份" align="center" prop="salaryMonth" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.salaryMonth, '{y}-{m}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="基本工资" align="center" prop="basicSalary" >
        <template slot-scope="scope">
          <span>{{ scope.row.basicSalary }}元</span>
        </template>
      </el-table-column>
      <el-table-column label="奖金" align="center" prop="bonus" >
        <template slot-scope="scope">
          <span>{{ scope.row.bonus }}元</span>
        </template>
      </el-table-column>
      <el-table-column label="津贴" align="center" prop="allowances" >
        <template slot-scope="scope">
          <span>{{ scope.row.allowances }}元</span>
        </template>
      </el-table-column>
      <el-table-column label="加班小时数" align="center" prop="overtimeHours" >
        <template slot-scope="scope">
          <span>{{ scope.row.overtimeHours === -1? '未填写':scope.row.overtimeHours }}h</span>
        </template>
      </el-table-column>
      <el-table-column label="加班费" align="center" prop="overtimePay" >
        <template slot-scope="scope">
          <span>{{ scope.row.overtimePay}}元</span>
        </template>
      </el-table-column>
      <el-table-column label="总薪资" align="center" prop="totalSalary" >
        <template slot-scope="scope">
          <span>{{ scope.row.totalSalary }}元</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['ldcm:salaryrecords:edit']"
          >修改</el-button> -->
          <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['ldcm:salaryrecords:remove']"
          >删除</el-button> -->
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改薪资记录对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="90px">
        <el-form-item label="姓名" prop="laborerId">
          <el-select v-model="form.laborerId" placeholder="请选择劳务人员" disabled>
            <el-option
              v-for="dict in laborersList"
              :key="dict.id"
              :label="dict.name"
              :value="dict.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="薪资月份" prop="salaryMonth" >
          <el-date-picker clearable
            v-model="form.salaryMonth"
            type="date"
            value-format="yyyy-MM"
            placeholder="请选择薪资月份"
            disabled>
          </el-date-picker>
        </el-form-item>
        <el-form-item label="加班小时数" prop="overtimeHours">
          <el-input v-model="form.overtimeHours" placeholder="请输入加班小时数" >
          </el-input>
        </el-form-item>
        <el-form-item label="总薪资" prop="totalSalary">
          <el-input v-model="form.totalSalary" placeholder="请输入总薪资" disabled/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listSalaryrecords, getSalaryrecords, delSalaryrecords, addSalaryrecords, updateSalaryrecords } from "@/api/ldcm/salaryrecords";
import { listLaborers } from "@/api/ldcm/laborers";
export default {
  name: "Salaryrecords",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 薪资记录表格数据
      salaryrecordsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        laborerId: null,
        salaryMonth: null,
        totalSalary: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        laborerId: [
          { required: true, message: "劳务人员ID不能为空", trigger: "blur" }
        ],
        salaryMonth: [
          { required: true, message: "薪资月份不能为空", trigger: "blur" }
        ],
        totalSalary: [
          { required: true, message: "总薪资不能为空", trigger: "blur" }
        ],
      },
      laborersList: [],
    };
  },
  created() {
    this.getList();
    this.getLaborers();
  },
  methods: {
    /** 查询薪资记录列表 */
    getList() {
      this.loading = true;
      listSalaryrecords(this.queryParams).then(response => {
        this.salaryrecordsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        recordId: null,
        laborerId: null,
        salaryMonth: null,
        basicSalary: null,
        bonus: null,
        allowances: null,
        overtimeHours: null,
        overtimePay: null,
        totalSalary: null,
        createBy: null,
        createdAt: null,
        updateBy: null,
        updatedAt: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.recordId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加薪资记录";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const recordId = row.recordId || this.ids
      getSalaryrecords(recordId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改薪资记录";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.recordId != null) {
            updateSalaryrecords(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addSalaryrecords(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const recordIds = row.recordId || this.ids;
      this.$modal.confirm('是否确认删除薪资记录编号为"' + recordIds + '"的数据项？').then(function() {
        return delSalaryrecords(recordIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('ldcm/salaryrecords/export', {
        ...this.queryParams
      }, `salaryrecords_${new Date().getTime()}.xlsx`)
    },
    //获取劳务人员
    getLaborers(){
      listLaborers({ pageNum: 1, pageSize: 10000 }).then(response => {
        console.log(response.rows);
        this.laborersList = response.rows;
      });
    },
  }
};
</script>
